/**
 * Universita' degli Studi di Palermo, 2010-11, License GNU GPL 3 (http://www.gnu.org/licenses/gpl.html)
 */
package org.unipa.botplatform;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.unipa.botplatform.bot.BotContext;
import org.unipa.botplatform.util.FileManager;

/**
 * CoreSettings contains the basic properties of a Core, including default paths, beans name and 
 * default BotContext.
 * 
 * @author Giuseppe Miceli
 *
 */
public class CoreSettings {

	/** Properties */
	private Properties properties;

	/** URL of the beans file */
	protected URL beansURL;

	/** URL of the bots definition file */
	protected URL botsURL;

	/** XML schema of the bots file */
	protected String botsSchema;

	/** URL of the XML schema of the bots file */
	protected URL botsSchemaURL;

	/** Bean id of the bot parser */
	protected String botParserBeanId;

	/** Bean id of the bot implementation */
	protected String botBeanId;

	/** Bean id of the planner implementation */
	protected String plannerBeanId;	

	/** Bean id of the session implementation */
	protected String sessionBeanId;	

	/** Default bot context */
	protected BotContext defaultBotCxt;

	/**
	 * @param url URL path of the properties file. Properties file must respect the 
	 * http://java.sun.com/dtd/properties.dtd XML schema.
	 * @throws IOException 
	 * @throws InvalidPropertiesFormatException 
	 * @throws CoreSettingsInitializationException 
	 */
	public CoreSettings(URL url) throws InvalidPropertiesFormatException, IOException {

		properties = new Properties();
		this.properties.loadFromXML(url.openStream());

		init();
	}

	/**
	 * Initialization method. It loads the basic properties, the bots and the contexts definitions.
	 * @throws CoreSettingsInitializationException 
	 * @throws MalformedURLException 
	 */
	private void init() throws MalformedURLException {

		// Beans
		String beansPath = properties.getProperty("beansURL");		
		this.beansURL = FileManager.getValidURL(beansPath);

		// Bots file URL
		String botsPath = properties.getProperty("botsURL");
		this.botsURL = FileManager.getValidURL(botsPath);	

		// Bots Schema
		this.botsSchema = properties.getProperty("botsSchema");

		String botsSchemaPath = properties.getProperty("botsSchemaURL");
		this.botsSchemaURL = FileManager.getValidURL(botsSchemaPath);	

		this.botParserBeanId = properties.getProperty("botParserBeanId");

		this.defaultBotCxt = new BotContext();

		this.botBeanId = properties.getProperty("defaultBotBeanId");
		this.defaultBotCxt.setBotBean(botBeanId);

		this.plannerBeanId = properties.getProperty("defaultPlannerBeanId");
		this.defaultBotCxt.setPlannerBean(plannerBeanId);

		this.sessionBeanId = properties.getProperty("defaultSessionBeanId");
		this.defaultBotCxt.setSessionBean(sessionBeanId);
	}
}
